import { create } from 'zustand'
import { immer } from 'zustand/middleware/immer'
import { devtools, persist, subscribeWithSelector } from 'zustand/middleware'

const initialUserValue = {
	token: '',
	userInfo: {},
}
export const useUserStore = create()(
	immer(
		devtools(
			subscribeWithSelector(
				persist(() => initialUserValue, {
					name: 'userStore',
				})
			)
		)
	)
)

export const setToken = (token) => {
	useUserStore.setState(() => ({ token }))
}
export const setUserInfo = (userInfo) => {
	useUserStore.setState(() => ({ userInfo }))
}
export const reset = () => {
	useUserStore.setState(() => ({ token: '' }))
}
